<template>
  <div class="register-container">
    <Canvas id="canvas"></Canvas>
    <div class="register">
      <div class="regist-form">
        <h2>账号注册</h2>
        <div class="inputitem">
          <i class="icomoon ico-name"></i>
          <input type="text" placeholder="请输入姓名" v-model="name" />
        </div>
        <div class="inputitem">
          <i class="icomoon ico-user"></i>
          <input
            type="text"
            placeholder="学生账号为8位数字，教师注册请以ta开头+8位数字"
            v-model="account"
          />
        </div>
        <div class="inputitem">
          <i class="icomoon ico-pwd"></i>
          <input type="password" placeholder="请输入注册密码" v-model="pwd" />
        </div>
        <button class="register-button" @click="register">点击注册</button>
      </div>
    </div>
  </div>
</template>

<script>
import { init } from "@/views/Register/rain/index";
import http from "@/api/registerapi.js";
import check from "@/views/Register/checkvalid";
export default {
  name: "",
  data() {
    return {
      ctx: {},
      name: "",
      account: "",
      pwd: "",
    };
  },
  methods: {
    initCanvas() {
      const canvas = document.querySelector("#canvas");
      this.ctx = canvas.getContext("2d");
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      init(this.ctx);
    },
    async register() {
      let msgresult=check(this.name, this.account, this.pwd);
      if (msgresult.result) {
        let currenttime=new Date();
        let year=currenttime.getFullYear();
        let month=currenttime.getMonth()+1;
        let day=currenttime.getDate();
        let hour=currenttime.getHours();
        let minutes=currenttime.getMinutes();
        minutes=minutes< 10 ? "0"+minutes : minutes;  //若分钟小于10，则前面补0
        let seconds=currenttime.getSeconds();
        seconds=seconds < 10 ? "0"+seconds : seconds;//若秒钟小于10，则前面补0
        let time=`${year}-${month}-${day} ${hour}:${minutes}:${seconds}`;
        let res = await http.sendregister({
          name: this.name,
          account: this.account,
          pwd: this.pwd,
          time:time
        });
        if (res.status == 200) {
          this.$Toast({
            content: res.data.msg,
            type: res.data.type,
          });
        }
        if(res.data.type=="success"){
                  setTimeout(() => {
        this.$router.push("login");
      }, 3000);
        }
      }
      else{
        this.$Toast({
          content: msgresult.msg,
          type: "error"
        })
      }
    },
  },
  components: {},
  mounted() {
    this.initCanvas();
  },
};
</script>

<style lang="less" scoped>
.register-container {
  width: 100%;
  height: 100%;
  position: absolute;
  background: url("@/views/Register/rain/images/rainimg.jpeg") no-repeat center
    center;
  background-size: cover;
  .register {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
    .regist-form {
      width: 500px;
      margin: 0 auto;
      padding: 35px 30px 35px;
      background-color: rgba(0, 0, 0, 0.5);
      border-radius: 10px;
      h2 {
        color: white;
        text-align: center;
        font-size: 16px;
        font-weight: normal;
        margin-bottom: 20px;
      }
      .inputitem {
        display: flex;
        align-items: center;
        margin-bottom: 20px;
        border-bottom: 1px solid #ccc;
        i {
          margin-right: 10px;
          color: white;
          &.ico-user::before {
            content: "\e971";
          }
          &.ico-pwd::before {
            content: "\e98d";
          }
          &.ico-name::before {
            content: "\e9b2";
          }
        }
        input {
          width: 100%;
          color: #fff;
          outline: none;
          border: none;
          background-color: transparent;
          &::placeholder {
            color: #fff;
          }
        }
      }
      button {
        display: block;
        text-align: center;
        width: 500px;
        padding: 10px 0;
        background-color: transparent;
        border: 1px solid white;
        color: white;
        cursor: pointer;
        &:hover {
          background: #0090ff;
          border-color: #0090ff;
        }
      }
    }
  }
}
</style>
